package com.efounder.form.service;

import android.util.Log;

import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.base.json.JSONUtil;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.base.DataComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.builder.IScriptObject;
import com.efounder.frame.utils.EFAppAccountUtils;
import com.efounder.service.IService;
import com.efounder.utils.FileUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Javor on 16/9/20.
 */

public class JSONFormModelLoadService extends DataComponent implements IService, IComponent, IScriptObject {
    private String fileName;

    private Map scriptObject = new HashMap();
    private Map scriptContext = new HashMap();

    @Override
    public Object executeService(JParamObject po, Object dao, Object co, Object userObject, Object ao) {
        String json = FileUtils.readTextFile(EFAppAccountUtils.getAppAccountJsonPath() +"/" + fileName);
        if (json == null || "".equals(json)) {
            Log.e("!!!!!","!!!!!json文件没读取到："+fileName);
            return null;
        }
        EFRowSet rowSet = JSONUtil.JSON2RowSet(json);
        return rowSet;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    private String id;

    @Override
    public String getID() {
        return id;
    }

    @Override
    public void setID(String id) {
        this.id = id;
    }

    @Override
    public void creationComplete() {

    }

    @Override
    public Map getScriptObject() {
        return scriptObject;
    }

    @Override
    public void setScriptObject(Map scriptObject) {
        this.scriptObject = scriptObject;
    }

    @Override
    public String getEventScript(String key) {
        return (String) scriptObject.get(key);
    }

    @Override
    public void setEventScript(String key, String script) {
        scriptObject.put(key, script);
    }

    @Override
    public Map getScriptContext() {
        return scriptContext;
    }

    @Override
    public void setScriptContext(Map context) {
        scriptContext = context;
    }
}
